package com.xctech.yace.factory;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.Properties;

/**
 * sqlsession factory 单例  事务设置为手动提交
 */
public class MybatisSessionFactory {

    private static Logger logger = LoggerFactory.getLogger(MybatisSessionFactory.class);

    private static SqlSessionFactory sqlSessionFactory;

    private static SqlSessionFactory originFactory;
    private static SqlSessionFactory targetFactory;

    private MybatisSessionFactory() {
        super();
    }

    public synchronized static SqlSessionFactory getSqlSessionFactory(Properties props) {
        if (null == sqlSessionFactory) {
            InputStream inputStream = null;
            try {
                inputStream = MybatisSessionFactory.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, props);
            } catch (Exception e) {
                logger.error("create MybatisSessionFactory read mybatis-config.xml cause Exception", e);
            }
            if (null != sqlSessionFactory) {
                logger.info("get Mybatis sqlsession sucessed....");
            } else {
                logger.info("get Mybatis sqlsession failed....");
            }
        }
        return sqlSessionFactory;
    }

    public synchronized static SqlSessionFactory getFactory(SqlSessionFactory sqlSessionFactory, Properties props) {
        if (sqlSessionFactory == null) {
            InputStream inputStream;
            try {
                inputStream = MybatisSessionFactory.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, props);
            } catch (Exception e) {
                logger.error("create MybatisSessionFactory read mybatis-config.xml cause Exception", e);
            }
            if (null != sqlSessionFactory) {
                logger.info("get Mybatis sqlsession sucessed....");
            } else {
                logger.info("get Mybatis sqlsession failed....");
            }
        }
        return sqlSessionFactory;
    }



    public static SqlSessionFactory getOriginFactory(Properties params) {
        Properties origin = new Properties();
        origin.setProperty("driver", params.getProperty("origin.jdbc.connector.driver"));
        origin.setProperty("url", params.getProperty("origin.jdbc.connector.url"));
        origin.setProperty("username", params.getProperty("origin.jdbc.connector.username"));
        origin.setProperty("password", params.getProperty("origin.jdbc.connector.password"));
        return MybatisSessionFactory.getFactory(originFactory, origin);

    }

    public static SqlSessionFactory getTargetFactory(Properties params) {
        Properties target = new Properties();
        target.setProperty("driver", params.getProperty("target.jdbc.connector.driver"));
        target.setProperty("url", params.getProperty("target.jdbc.connector.url"));
        target.setProperty("username", params.getProperty("target.jdbc.connector.username"));
        target.setProperty("password", params.getProperty("target.jdbc.connector.password"));
        return MybatisSessionFactory.getFactory(targetFactory, target);
    }

}
